package com.ideaaedi.springcloud.jd.commonspring.registrar;

import com.ideaaedi.springcloud.jd.commonspring.annotation.EnableJdFeature;
import com.ideaaedi.springcloud.jd.commonspring.config.XxlJobConfig;
import com.ideaaedi.springcloud.jd.commonspring.enhancer.FillReturnAdvice;
import com.ideaaedi.springcloud.jd.commonspring.refresher.ApiResourceRefresher;
import com.ideaaedi.springcloud.jd.commonspring.support.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.NonNull;

import java.util.Map;
import java.util.Objects;

/**
 * 功能注册器
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.5.A
 */
@Slf4j
public class FeatureJdRegistrar implements ImportBeanDefinitionRegistrar {
    
    /**
     * 注册bean
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, @NonNull BeanDefinitionRegistry registry) {
        Map<String, Object> attributeMap = annotationMetadata.getAnnotationAttributes(EnableJdFeature.class.getName());
        Objects.requireNonNull(attributeMap, "attributeMap should not be null.");
        boolean enableCommonService = (boolean) attributeMap.get("enableCommonService");
        boolean enableApiRefresher = (boolean) attributeMap.get("enableApiRefresher");
        boolean enableXxlJobConfig = (boolean) attributeMap.get("enableXxlJobConfig");
        boolean enableFillReturnAdvice = (boolean) attributeMap.get("enableFillReturnAdvice");
        // 注册
        if (enableCommonService) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CommonService.class);
            registry.registerBeanDefinition(CommonService.BEAN_NAME, builder.getBeanDefinition());
            log.info("registry bean '{}' with class '{}'", CommonService.BEAN_NAME, CommonService.class.getName());
        }
        if (enableApiRefresher) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ApiResourceRefresher.class);
            registry.registerBeanDefinition(ApiResourceRefresher.BEAN_NAME, builder.getBeanDefinition());
            log.info("registry bean '{}' with class '{}'", ApiResourceRefresher.BEAN_NAME, ApiResourceRefresher.class.getName());
        }
        if (enableXxlJobConfig) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XxlJobConfig.class);
            registry.registerBeanDefinition(XxlJobConfig.BEAN_NAME, builder.getBeanDefinition());
            log.info("registry bean '{}' with class '{}'", XxlJobConfig.BEAN_NAME, XxlJobConfig.class.getName());
        }
        if (enableFillReturnAdvice) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FillReturnAdvice.class);
            registry.registerBeanDefinition(FillReturnAdvice.BEAN_NAME, builder.getBeanDefinition());
            log.info("registry bean '{}' with class '{}'", FillReturnAdvice.BEAN_NAME, FillReturnAdvice.class.getName());
        }
    }
}
